package com.mc.mcgoweb.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;

/**
 * User: liaowj
 * Date: 2016-06-27
 * Time: 16:32
 */
public class NullEleToEmptySerializer extends JsonSerializer<ArrayList> {

    @Override
    public void serialize(ArrayList value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        jgen.writeStartArray();
        if (!CollectionUtils.isEmpty(value)) {
            for (final Object ele : value) {
                if (ele == null) {
                    jgen.writeStartObject();
                    jgen.writeEndObject();
                } else {
                    jgen.writeObject(ele);
                }
            }
        }
        jgen.writeEndArray();
    }
}